package cc.mrbird.febs.accounting.service.impl;

import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.CompanyProject;
import cc.mrbird.febs.accounting.mapper.CompanyProjectMapper;
import cc.mrbird.febs.accounting.service.ICompanyProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  Service实现
 *
 * @author baoyinlei
 * @date 2021-09-28 23:26:58
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CompanyProjectServiceImpl extends ServiceImpl<CompanyProjectMapper, CompanyProject> implements ICompanyProjectService {

    private final CompanyProjectMapper companyProjectMapper;

    @Override
    public IPage<CompanyProject> findCompanyProjects(QueryRequest request, CompanyProject companyProject) {
        LambdaQueryWrapper<CompanyProject> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<CompanyProject> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<CompanyProject> findCompanyProjects(CompanyProject companyProject) {
	    LambdaQueryWrapper<CompanyProject> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCompanyProject(CompanyProject companyProject) {
        this.save(companyProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCompanyProject(CompanyProject companyProject) {
        this.saveOrUpdate(companyProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCompanyProject(CompanyProject companyProject) {
        LambdaQueryWrapper<CompanyProject> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    public List<Long> getCompanyIdsByPid(Long projectId) {
        LambdaQueryWrapper<CompanyProject> q = new LambdaQueryWrapper<>();
        q.eq(CompanyProject::getProjectId,projectId);
        return this.baseMapper.selectList(q)
                .stream()
                .map(CompanyProject::getCompanyId)
                .collect(Collectors.toList());
    }
}
